export default {

    Time(timestamp) {
        if (typeof timestamp !== 'number') {
            //throw new Error('Invalid argument: timestamp should be a positive number.');  
            timestamp = +timestamp;
        }

        const now = Math.floor(Date.now() / 1000); // 注意这里除以1000来获取秒  
        const difference = now - timestamp;

        const formatTime = (hours, minutes) => {
            return `${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}`;
        };

        if (difference < 60) {
            return `${difference}秒前`;
        } else if (difference < 60 * 60) { // 分钟  
            const minutes = Math.floor(difference / 60);
            return `${minutes}分钟前`;
        } else if (difference < 24 * 60 * 60) { // 小时  
            const hours = Math.floor(difference / (60 * 60));
            return `${hours}小时前`;
        } else if (difference < 7 * 24 * 60 * 60) { // 天  
            const days = Math.floor(difference / (24 * 60 * 60));
            return `${days}天前`;
        } else if (difference < 4 * 7 * 24 * 60 * 60) { // 4周内  
            const days = Math.floor(difference / (24 * 60 * 60));
            const weeks = Math.floor(days / 7);
            const remainingDays = days % 7;
            if (remainingDays > 0) {
                return `${weeks}周${remainingDays}天前`;
            } else {
                return `${weeks}周前`;
            }
        } else {
            const date = new Date(timestamp * 1000);
            const year = date.getFullYear();
            const month = (date.getMonth() + 1).toString().padStart(2, '0'); // 月份需要前导零  
            const day = date.getDate().toString().padStart(2, '0'); // 日期需要前导零  

            if (year === new Date().getFullYear()) {
                return `${month}月${day}日 ${formatTime(date.getHours(), date.getMinutes())}`;
            } else {
                return `${year}年${month}月${day}日 ${formatTime(date.getHours(), date.getMinutes())}`;
            }
        }
    },
    UtcToUnix: (utc) => {
        const date = new Date(utc);
        return Math.floor(date / 1000);
    },
    debounce(fn, delay) {
        let timeoutID;
        return function (...args) {
            clearTimeout(timeoutID);
            timeoutID = setTimeout(() => {
                fn.apply(this, args);
            }, delay);
        }
    },
    formatFileSize(fileSize) {
        let units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'];
        let i = 0;
        while (fileSize > 1024) {
            fileSize /= 1024;
            i++;
        }
        // 保留两位小数  
        return fileSize.toFixed(2) + ' ' + units[i];
    }
}